<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="任务类型" prop="taskType">
        <el-select style="width: 150px" v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
          <el-option v-for="dict in taskTypeList" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分行名称" prop="branchName">
        <el-input
          style="width: 150px"
          v-model="queryParams.branchName"
          placeholder="请输入分行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支行名称" prop="deptName">
        <el-input
          style="width: 150px"
          v-model="queryParams.deptName"
          placeholder="请输入支行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="摄像头名称" prop="cameraName">
        <el-input
          v-model="queryParams.cameraName"
          placeholder="请输入摄像头名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="场景名称" prop="scenariosTypeList">
        <el-select v-model="queryParams.scenariosTypeList" multiple placeholder="请选择场景" style="width: 150px;">
          <el-option v-for="dict in dict.type.task_scenarios_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否复核" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择复核状态">
          <el-option key="0" label="待复核" value="0"></el-option>
          <el-option key="1" label="已复核" value="1"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="创建日期" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          style="width: 220px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['task:risk:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['task:risk:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:risk:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['task:risk:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="任务ID" align="center" prop="taskId" /> -->
      <!-- <el-table-column label="分行编号" align="center" prop="branchCode" /> -->
      <el-table-column label="任务类型" align="center" prop="taskType" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.taskType | handleTaskType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分行名称" align="center" prop="branchName" />
      <!-- <el-table-column label="支行编号" align="center" prop="deptCode" /> -->
      <el-table-column label="支行名称" align="center" prop="deptName" />
      <el-table-column label="摄像头名称" align="center" prop="cameraName" />
      <!-- <el-table-column label="场景编号" align="center" prop="scenariosType" /> -->
      <el-table-column label="场景名称" align="center" prop="scenariosName" />
      <el-table-column label="风险原因" align="center" prop="riskReason" />
      <el-table-column label="更新时间" align="center" prop="updateTime"  width="180"/>
      <el-table-column label="风险开始时间" align="center" prop="startTime" width="180"></el-table-column>
      <el-table-column label="风险结束时间" align="center" prop="endTime" width="180"></el-table-column>
      <!-- <el-table-column label="是否复核" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.checkStatus === "1" ? "已复核" : "待复核" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否违规" align="center" prop="resultStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.resultStatus | handleResultStatus }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleQueryRiskImages(scope.row)"
          >查看图片</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="900px">
      <el-carousel :autoplay="false" arrow="always" trigger="click" height="500px">
        <el-carousel-item v-for="(item, index) in riskImages" :key="index">
          <div style="display: flex;justify-content: center;align-items: center;width: 100%;">
            <img :src="item" style="width: 100%;">
          </div>
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>
</template>

<script>
import { createCsv, createUniqueString } from "@/utils/index";
import { listRisk, getRisk, delRisk, addRisk, updateRisk, exportTemp, queryRiskImages } from "@/api/iis/riskResult";
export default {
  name: "Risk",
  dicts: ['sys_normal_disable','task_frequency_type',"task_scenarios_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务风险分析结果明细表格数据
      riskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // taskId: null,
        // branchCode: null,
        branchName: null,
        // deptCode: null,
        // deptName: null,
        // cameraName: null,
        scenariosType: null,
        // scenariosName: null,
        scenariosTypeList: [],
        // riskReason: null,
        // startTime: null,
        // endTime: null,
        checkStatus: null, // 0-待复核，1-已复核
        // resultStatus: null, // 0-不违规，1-违规
        // status: null,
        timeRange: [],
        taskType: ""
      },
      taskTypeList: [
        {
          value: "1",
          label: "总行任务"
        },
        {
          value: "2",
          label: "分行任务"
        },
        {
          value: "3",
          label: "浩云分行"
        }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "唯一UUID不能为空", trigger: "blur" }
        ],
      },
      dialogVisible: false,
      riskImages: []
    };
  },
  created() {
    this.getList();
  },
  filters: {
    handleResultStatus(resultStatus) {
      if (typeof resultStatus === null || resultStatus === "") {
        return "";
      }else if (resultStatus === "0") {
        return "不违规";
      } else if (resultStatus === "1") {
        return "违规";
      }
    },
    handleTaskType(taskType) {
      if (taskType === "1") {
        return "总行任务";
      } else if (taskType === "2") {
        return "分行任务";
      }
    },
  },
  methods: {
    handleQueryRiskImages(row) {
      this.riskImages = []
      queryRiskImages(row.id).then(response => {
        this.riskImages = response.data;
        if (this.riskImages && this.riskImages.length > 0) {
          this.dialogVisible = true;
        }else {
          this.dialogVisible = false;
          this.$message.warning("暂无图片数据");
        }
      });
    },
    /** 查询任务风险分析结果明细列表 */
    getList() {
      this.loading = true;
      let queryParams = Object.assign({}, this.queryParams);
      if (queryParams.scenariosTypeList && queryParams.scenariosTypeList.length > 0) {
        queryParams.scenariosType = queryParams.scenariosTypeList.join(",");
      }
      if (queryParams.timeRange && queryParams.timeRange.length > 0) {
        queryParams.startTime = queryParams.timeRange[0];
        queryParams.endTime = queryParams.timeRange[1];
      }
      delete queryParams.scenariosTypeList;
      delete queryParams.timeRange;
      listRisk(queryParams).then(response => {
        this.riskList = response.rows;
        this.total = response.total;
        this.loading = false;

        // const meta = [
        //   "表头1",
        //   "表头2",
        //   "表头3",
        //   "表头4",
        //   "表头5",
        //   "表头6",
        //   "表头7",
        // ]
        // const data = [
        //   [
        //     "行数1-1",
        //     "行数1-2",
        //     "行数1-3",
        //     "行数1-4",
        //     "行数1-5",
        //     "行数1-6",
        //     "行数1-7"
        //   ],
        //   [
        //     "行数2-1",
        //     "行数2-2",
        //     "行数2-3",
        //     "行数2-4",
        //     "行数2-5",
        //     "行数2-6",
        //     "行数2-7"
        //   ],
        //   [
        //     "行数3-1",
        //     "行数3-2",
        //     "行数3-3",
        //     "行数3-4",
        //     "行数3-5",
        //     "行数3-6",
        //     "行数3-7"
        //   ],
        //   [
        //     "行数4-1",
        //     "行数4-2",
        //     "行数4-3",
        //     "行数4-4",
        //     "行数4-5",
        //     "行数4-6",
        //     "行数4-7"
        //   ],
        // ]
        // const csvName = "riskList"
        // createCsv(meta, data, csvName)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        priKey: null,
        id: null,
        taskId: null,
        branchCode: null,
        branchName: null,
        deptCode: null,
        deptName: null,
        cameraName: null,
        scenariosType: null,
        scenariosName: null,
        riskReason: null,
        startTime: null,
        endTime: null,
        checkStatus: null,
        resultStatus: null,
        remark: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.priKey)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加任务风险分析结果明细";
    // },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const priKey = row.priKey || this.ids
    //   getRisk(priKey).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改任务风险分析结果明细";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.priKey != null) {
            updateRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const priKeys = row.priKey || this.ids;
      this.$modal.confirm('是否确认删除任务风险分析结果明细编号为"' + priKeys + '"的数据项？').then(function() {
        return delRisk(priKeys);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = Object.assign({}, this.queryParams);
      if (queryParams.scenariosTypeList && queryParams.scenariosTypeList.length > 0) {
        queryParams.scenariosType = queryParams.scenariosTypeList.join(",");
      }
      if (queryParams.timeRange && queryParams.timeRange.length > 0) {
        queryParams.startTime = queryParams.timeRange[0];
        queryParams.endTime = queryParams.timeRange[1];
      }
      delete queryParams.scenariosTypeList;
      delete queryParams.timeRange;
      this.download('task/risk/export', {
        ...queryParams
      }, `risk_${new Date().getTime()}.csv`)

      // exportTemp(queryParams).then(response => {
      //   const responseData = response.data || {
      //     meta: [],
      //     data: []
      //   }
      //   const meta = responseData.meta
      //   const data = responseData.data
      //   const csvName = "riskList-" + createUniqueString()
      //   createCsv(meta, data, csvName)
      // });
    },
    handleConfirmRisk() {
    },
    handleNotRisk() {
    },
  }
};
</script>
